﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using B4Deal.Models;

namespace B4Deal.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Account/
        MyDataMembershipProvider membership = new MyDataMembershipProvider();
        public ActionResult Index()
        {
            return RedirectToAction("Index", "Home");
        }

        [HttpPost]
        public ActionResult Logon(String email, String pass, bool remember)
        {
            if (Request.IsAjaxRequest())
            {
                bool sc = membership.ValidateUser(email, pass);
                if (sc)
                {
                    Session["user"] = email;
                    Session["logon"] = "on";
                    FormsAuthentication.SetAuthCookie(email, remember);
                    return Json(new { rs = "success", detail = "Logon successfully!" });
                }
                FormsAuthentication.SetAuthCookie(email, false);
                return Json(new { rs = "fail", detail = "Tài khoản hoặc mật khẩu không hợp lệ!" });
            }
            return RedirectToAction("Index", "Home");
        }
        
        public ActionResult Logoff(string act, string con)
        {
            Session.Clear();
            //clear cookie
            if (this.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("Cookie"))
            {
                HttpCookie cookie = this.ControllerContext.HttpContext.Request.Cookies["Cookie"];
                cookie.Expires = DateTime.Now.AddDays(-1);
                this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
            }            
            return Json(new {rs = "success"});
        }

        [HttpPost]
        public ActionResult Register()
        {
            if (Request.IsAjaxRequest())
            {
                String email = Request.Form["email"].ToString();
                String pass = Request.Form["password"].ToString();
                String name = Request.Form["username"].ToString();
                String terms = null;
                if (Request.Form.Count > 4)
                    terms = Request.Form["terms"].ToString();
                if (terms != null && terms.Equals("on"))
                {
                    try
                    {
                        membership.CreateUser(email, pass, name);
                    }
                    catch (Exception ex)
                    {
                        return Json(new { rs = "fail", detail = ex.Message });
                    }
                    FormsAuthentication.SetAuthCookie(email, true);
                    Session["user"] = email;
                    Session["logon"] = "on";
                    return Json(new { rs = "success", detail = "Register successfully!" });
                }
                else
                {
                    return Json(new { rs = "fail", detail = "Bạn chưa chấp nhận điều khoản!" });
                }
            }
            return RedirectToAction("Index", "Home");
        }
    }
}
